package com.xps.charminguser.view.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.xps.charminguser.R;
import com.xps.charminguser.bean.LoginBean;
import com.xps.charminguser.date.net.UserNetWorks;
import com.xps.charminguser.view.base.BaseActivity;
import com.xps.charminguser.view.fragment.BillFragment;
import com.xps.charminguser.view.fragment.CompleteFragment;
import com.xps.charminguser.view.fragment.ConductFragment;
import com.xps.charminguser.view.fragment.ListFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by 吾丶说谎者 on 2017/11/21.
 */

public class MainActivity extends BaseActivity {


    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.main_container)
    FrameLayout mainContainer;
    @BindView(R.id.tv_main)
    TextView tvMain;
    @BindView(R.id.tv_dynamic)
    TextView tvDynamic;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    ListFragment listFragment;
    CompleteFragment completeFragment;
    ConductFragment conductFragment;
    BillFragment billFragment;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void setData() {
        actionbarIvBack.setVisibility(View.GONE);
        actionbarTvTitle.setText("首页");
        tvMain.setSelected(true);
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        listFragment = new ListFragment();
        completeFragment = new CompleteFragment();
        conductFragment = new ConductFragment();
        billFragment = new BillFragment();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, listFragment).disallowAddToBackStack().commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, completeFragment).disallowAddToBackStack().show(completeFragment).commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, conductFragment).disallowAddToBackStack().show(conductFragment).commit();
        this.getSupportFragmentManager().beginTransaction().add(R.id.main_container, billFragment).disallowAddToBackStack().show(billFragment).commit();
        completeFragment.hide();
        conductFragment.hide();
        billFragment.hide();
    }


    public void NotInstalled() {
        UserNetWorks.UserLogin("liwenhan", "123456", new Subscriber<LoginBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(LoginBean loginBean) {
                dissmisDialog();
                if (loginBean.getReturn_code().equals("FALL")) {
                    JUtils.Toast("登陆失败");
                } else {
                    JUtils.Toast("登陆成功");
                }
            }
        });
    }


    @OnClick({R.id.tv_main, R.id.tv_dynamic, R.id.tv_message, R.id.tv_person})
    public void onViewClicked(View view) {
        tvMain.setSelected(false);
        tvDynamic.setSelected(false);
        tvMessage.setSelected(false);
        tvPerson.setSelected(false);
        switch (view.getId()) {
            case R.id.tv_main:
                tvMain.setSelected(true);
                actionbarTvTitle.setText("首页");
                showFragment(listFragment);
                break;
            case R.id.tv_dynamic:
                tvDynamic.setSelected(true);
                actionbarTvTitle.setText("我的订单");
                showFragment(completeFragment);
                break;
            case R.id.tv_message:
                tvMessage.setSelected(true);
                actionbarTvTitle.setText("订单列表");
                showFragment(conductFragment);
                break;
            case R.id.tv_person:
                tvPerson.setSelected(true);
                actionbarTvTitle.setText("我的账单");
                showFragment(billFragment);
                break;
        }
    }

    public void showFragment(Fragment showFragemt) {
        hideFrags();
        this.getSupportFragmentManager().beginTransaction().disallowAddToBackStack().show(showFragemt).commit();
    }

    public void hideFrags() {
        listFragment.hide();
        completeFragment.hide();
        conductFragment.hide();
        billFragment.hide();
    }

}